package pers.bc.utils.spring;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import pers.bc.utils.pub.LoggerUtil;
import pers.bc.utils.pub.StringUtil;

import javax.annotation.PostConstruct;

import java.util.HashSet; 
import java.util.Map;
import java.util.Set; 

@Configuration
public class MappingInspector
{
    
    @Autowired 
    private RequestMappingHandlerMapping requestMappingHandlerMapping;
    
    String folderName = "mapping";
    LoggerUtil loger = LoggerUtil.getInstance(folderName);
    
    @PostConstruct // 在Bean初始化后执行
    public void printMappings()
    {
        
        Set<String> minfo = new HashSet<String>();
        // 获取所有映射信息
        Map<RequestMappingInfo, HandlerMethod> handlerMethods = requestMappingHandlerMapping.getHandlerMethods();
        Set<RequestMappingInfo> requestMappingInfo = handlerMethods.keySet();
        for (RequestMappingInfo reqMapping : requestMappingInfo)
        {
            Set<String> patterns = reqMapping.getPatternsCondition().getPatterns();
            Set<RequestMethod> methods = reqMapping.getMethodsCondition().getMethods();
            
            System.out.println("=== 映射信息 ===");
            System.out.println("URL Pattern: " + StringUtil.valueOfEmpty(patterns));
            System.out.println("Handler Method: " + StringUtil.valueOfEmpty(methods));
            System.out.println("================\n");
            
            minfo.add("URL Pattern: " + StringUtil.valueOfEmpty(patterns)+" -> " + "Handler Method: " + StringUtil.valueOfEmpty(methods)+"\r\n");
            
        }
        loger.info("=========== 所有REST映射：==========");
        loger.info(StringUtil.valueOfEmpty(minfo));
        
        
//        // 格式化并打印输出
//        handlerMethods.forEach((key, value) -> {
//            System.out.println("=== 映射信息 ===");
//            System.out.println("URL Pattern: " + key);
//            System.out.println("Handler Method: " + value);
//            System.out.println("================\n");
//        });
//        
//        // 或者更简洁地按格式输出
//        System.out.println("所有REST映射：");
//        requestMappingHandlerMapping.getHandlerMethods().forEach((requestMappingInfo, handlerMethod) -> {
//            String patterns = requestMappingInfo.getPatternsCondition().getPatterns().stream().collect(Collectors.joining(", "));
//            String methods =
//                requestMappingInfo.getMethodsCondition().getMethods().stream().map(Enum::toString).collect(Collectors.joining(", "));
//            System.out.println(patterns + " [" + methods + "] -> " + handlerMethod.toString());
//        });
        
        loger.info(LoggerUtil.getSplitLine2());
        
    }
}
